-- treesitter 配置
require'nvim-treesitter.configs'.setup {
    ensure_installed = {
        "sql", "xml", "vim", "yaml", "c", "go", "lua", "markdown", "html",
        "css", "python", "vue", "javascript", "c_sharp", "gdscript", "rust",
        "typescript"
    },

    highlight = { enable = true },
    indent    = { enable = true },

    rainbow = {
        enable = true,
        extended_mode = false,
        max_file_lines = nil
    },
}

-- 启用 treesitter 代码折叠
vim.wo.foldmethod = 'expr'
vim.wo.foldexpr   = 'nvim_treesitter#foldexpr()'
vim.wo.foldlevel  = 99  -- 默认展开

-- 自动格式化并保持光标位置
local auto_indent = vim.api.nvim_create_augroup("AUTO_INDENT", { clear = true })

vim.api.nvim_create_autocmd("BufWritePost", {
    pattern = "*",
    group = auto_indent,
    callback = function()
        local pos = vim.api.nvim_win_get_cursor(0) -- 记录光标位置

        -- 如果支持 LSP 格式化，优先用 LSP
        local clients = vim.lsp.get_clients({ bufnr = 0 }) -- 修改为 get_clients
        local has_lsp_format = false
        for _, client in ipairs(clients) do
            if client.server_capabilities.documentFormattingProvider then
                has_lsp_format = true
                break
            end
        end

        if has_lsp_format then
            vim.lsp.buf.format({ async = false })
        else
            -- fallback：全文件缩进，但不影响 undo/jumps
            vim.cmd("silent! keepjumps normal! gg=G")
        end

        vim.api.nvim_win_set_cursor(0, pos) -- 恢复光标
    end,
})

